from django.shortcuts import render
from difflib import HtmlDiff

"""
待优化功能：
    1、界面需要优化
"""
def diff_comp(request):
    #通过POST请求获取前端传递的数据
    if request.method == 'POST':
        #通过html name属性值获取数据
        file_obj1 = request.FILES.get('file1')
        lins1 = []
        for chunk in file_obj1.chunks():
            lins1 = chunk.decode('utf-8').splitlines()

        file_obj2 = request.FILES.get('file2')
        lins2 = []
        for chunk in file_obj2.chunks():
            lins2 = chunk.decode('utf-8').splitlines()

        html_diff = HtmlDiff()
        result = html_diff.make_file(lins1, lins2)
        with open("net_dev/templates/diff_result.html", "w", encoding="utf8") as f:
            f.write(result)
        return render(request, "diff_result.html")
    return render(request, "difffile.html")